// mircoApp/package_deliver_write_off/package_deliver_write_off_add/package_deliver_write_off_add.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectDeliverId: null,
    showDeliverList: false,
    deliverList: [], //当前账号所属自提点
    list: [],
    deliver_count: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.code) {
      this.setData({
        code: options.code
      })
    }
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      if (scene) {
        let info_arr = [];
        info_arr = scene.split(',');
        for (let i = 0; i < info_arr.length; i++) {
          let chil_arr = [];
          chil_arr = info_arr[i].split('=');
          if (chil_arr[0] == "code") {
            this.data.code = chil_arr[1];
          }
        }
      }
    }
    this.getDeliverList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  async sureBtn() {
    let json = {
      code: this.data.code
    };
    if (this.data.deliver_count == 1) {
      json.id = this.data.selectDeliverId;
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.gradevin-stock.frontend.controllers.write-off.write-off"),
      data: json
    })
    if (data.result) {
      wx.navigateTo({
        url: '/mircoApp/package_deliver_write_off/package_deliver_write_off_index/package_deliver_write_off_index?Record=true',
      })
    } else {
      app.tips(data.msg)
    }
  },
  selectDeliverItem(e) {
    let id = e.currentTarget.dataset.id;
    if (this.data.selectDeliverId == id) {
      this.setData({
        selectDeliverId: null
      })
      return;
    }
    this.setData({
      selectDeliverId: id
    })
  },
  selectDeliver() {
    if (!this.data.selectDeliverId) {
      app.tips('请选择自提点')
      return;
    }
    this.setData({
      showDeliverList: false
    })
  },
  async getDeliverList() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.gradevin-stock.frontend.controllers.write-off.write-off-page"),
      data: {
        code: this.data.code
      }
    })
    if (data.result) {
      console.log(data.data)
      this.setData({
        list: data.data,
        deliver_count: false,
      })
      if (Number(data.data.deliver_count) > 1) {
        this.setData({
          deliver_count: true,
          showDeliverList: true,
        })
        this.getData();
      }
    } else {
      app.tips(data.msg)
    }
  },
  async getData() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.package-deliver-write-off.frontend.controllers.write-off.clerk-delivers")
    })
    if (data.result) {
      this.setData({
        deliverList: data.data.list
      })
    } else {
      app.tips(data.msg)
    }
  }
})